package com.seed.application.ghost;

public abstract class SeedGhost {

//    protected abstract HttpServletRequest getRequest();
//    protected abstract HttpServletResponse getResponse();
//
//    /**
//     * 执行幽灵方法
//     * uri 请求地址，形如： /, /action, /seed/action, /seed/action/pathVariable
//     * @throws IOException
//     * @throws ServletException
//     */
//    public R<?> exec() throws IOException {
//        Micro micro = Sa.getMicro();
//        String seedName = ActionEnv.getSeedName();
//        String actionName = ActionEnv.getActionName();
//        RequestMethod method = RequestMethod.valueOf(getRequest().getMethod());
//        if(StrUtil.isBlank(seedName)) {
//            Action action = SeedKit.getAction(micro.getSeed(seedName), method, actionName);
//            if(action == null) return null;
//
//            getRequest().setAttribute("seedName", action.getSeedName());
//            getRequest().setAttribute("actionName", actionName);
//            return execAction(action);
//        } else {
//            Map<String, Action> actions = micro.getActions();
//            if(actions == null) return null;
//
//            Action action = actions.get(actionName);
//            if(action == null || action.getMethods() == null || !(action.getMethods()).contains(method)) return null;
//
//            getRequest().setAttribute("seedName", seedName);
//            getRequest().setAttribute("actionName", actionName);
//            return execAction(action);
//        }
//    }
//
//    public R<?> execAction(Action action) throws IOException {
//        checkLogin();
//        checkPermisstion();
//
//        RequestMethod method = ActionEnv.getMethod();
//        String pathVariable = ActionEnv.getPathVariable();
//        SeedService seedService = Sa.getBean(SeedService.class);
////        if(action instanceof PageAction pageAction) {
////            return
////        }
////        ActionType actionType = action.getType();
//        if(actionType.equals(ActionType.page) && RequestMethod.GET == method) {
//            return R.data(seedService.page(getInt("pageNumber"), getInt("pageSize")));
//        }
//
//        if(actionType.equals(ActionType.save) && RequestMethod.POST == method) {
//            boolean b = seedService.save(readRequestBody(Row.class));
//            return b ? R.ok("操作成功") : R.error("操作失败");
//        }
//
//        if(actionType.equals(ActionType.update)) {
//            if(RequestMethod.GET == method) return R.data(seedService.detail(pathVariable));
//            if(RequestMethod.PUT == method) {
//                boolean b = seedService.update(readRequestBody(Row.class));
//                return b ? R.ok("操作成功") : R.error("操作失败");
//            }
//        }
//
//        if(actionType.equals(ActionType.detail) && RequestMethod.GET == method) {
//            return R.data(seedService.detail(pathVariable));
//        }
//
//        if(actionType.equals(ActionType.remove) && RequestMethod.DELETE == method) {
//            return R.data(seedService.remove(pathVariable));
//        }
//
//        if(actionType.equals(ActionType.set)) {
//            if(RequestMethod.GET == method) return R.data(seedService.remove(pathVariable));
//            if(RequestMethod.PUT == method) {
//                SetDTO dto = readRequestBody(SetDTO.class);
//                return R.data(seedService.set(dto));
//            }
//        }
//
//        return R.error("未实现的操作");
//    }
//
//    private Integer getInt(String name) {
//        String value = getRequest().getParameter(name);
//        if(StrUtil.isBlank(value)) return null;
//
//        return Integer.valueOf(value);
//    }
//
//    private <T> T readRequestBody(Class<T> clazz) throws IOException {
//        String json = getRequest().getReader().lines().collect(Collectors.joining());
//        return JSONUtil.toBean(json, clazz);
//    }
//
//    public void sendJSON(Object data) throws IOException {
//        HttpServletResponse response = getResponse();
//        response.setContentType("application/json");
//        response.setCharacterEncoding("UTF-8");
//        PrintWriter out = response.getWriter();
//        out.println(JSONUtil.toJsonStr(data));
//
//        out.flush();
//        out.close();
//    }
//
//    public void send404() throws IOException {
//        sendJSON(R.error("404"));
//    }
//
//    public void checkLogin() {
//
//    }
//
//    public void checkPermisstion() {
//
//    }
}
